package com.search.service;

import java.util.ArrayList;

import ue.dataSearch.indexer.Indexer;

import com.common.confManager.ConfManager;
import com.search.constants.SearchConstants;
import com.search.po.SearchBasePO;
import com.search.support.PostKeyAnalysisSupport;
import com.ue.data.DataRow;
import com.ue.data.DataTable;
import com.ue.log.LogManager;
import com.ue.util.StringHelper;

/**
 * 文 件 名: PostInfoSearchService.java 版 权: 深圳埃思欧纳信息咨询有限公司版权所有. YYYY-YYYY, All
 * rights reserved 描 述: <描述> 修 改 人: jeray.wu 修改时间: 2010-11-10 上午11:34:53 跟踪单号:
 * <跟踪单号> 修改单号: <修改单号> 修改内容: <修改内容>
 */
public class PostInfoSearchService
{
    private static ConfManager cm = ConfManager.getInstance();

    // 定义参与索引的字段
    private static String[] m_fields =
    {"post_alias", "ID", "corpID", "postName", "domainID", "areaID", "minReward", "maxReward", "intMinReward", "minYearPay", "maxYearPay","yearPay",
            "experience", "sex", "studyLevel", "postStatus", "positiontype", "has_anonymous", "deployTime","strDeployTime", "endTime",
            "startTime", "description", "capability", "updateTime", "corpName", "anony_name", "corpStatus",
            "minExperience", "maxExperience","mappath","fileServerId"};

    /**
     * <搜索职位信息>
     */
    public static DataTable getData(SearchBasePO sbpo)
    {
        // long l3 = new Date().getTime();

        DataTable dtData = new DataTable("DataTable");
        dtData.addColumn(m_fields);

        String strWhere = sbpo.getParamStr();
        if (StringHelper.isNullOrEmpty(strWhere))
        {
            return dtData;
        }
        // 先判断sbpo中的参数map!=null
        Indexer indexer = new Indexer(cm.getPropValue(SearchConstants.POSTINFO_PATH), null);
        // LogManager.debug("sbWhere=======  " + strWhere);

        try
        {
            dtData = indexer
                    .search(strWhere, sbpo.getOffset(), sbpo.getOffset() + sbpo.getLimit(), sbpo.getOrder_str());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        // LogManager.debug("总条数：" + dtData.getTotalRecords());
        // for (DataRow dr : dtData.getRows())
        // {
        // LogManager.debug(dr.getString("ID") + "==" + dr.getString("areaID") +
        // "职位名称：：" + dr.getString("postName")
        // + "::" + dr.getString("corpName") + "::" + dr.getString("endTime") +
        // "   :::   "
        // + dr.getString("experience") + "   :::   " +
        // dr.getString("studyLevel") + "   :::   "
        // + dr.getString("minExperience") + "   :::   " +
        // dr.getString("maxExperience"));
        // }
        // long l4 = new Date().getTime();
        // LogManager.debug("lucenec查询消耗时间为：" + (l4 - l3));
        return dtData;
    }

    /**
     * <分词搜索职位数据>
     * @param sbpo
     * 
     * @return DataTable [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static DataTable getDataKeyAnalysis(SearchBasePO sbpo)
    {

        DataTable dtData = new DataTable("DataTable");
        dtData.addColumn(m_fields);

        // 防止重复
        ArrayList<String> ids = new ArrayList<String>();

        // 建立搜索条件
        ArrayList<String> words = PostKeyAnalysisSupport.analysePostSearchKey(sbpo);
        // 总记录数
        Long lngRowCount = 0L;
        Indexer indexer = new Indexer(cm.getPropValue(SearchConstants.POSTINFO_PATH), null);
        try
        {
            // 搜索各分词结果
            for (String word : words)
            {
                DataTable dt;
                LogManager.debug("sbWhere=======  " + word);
                dt = indexer.search(word, sbpo.getOffset(), sbpo.getOffset() + sbpo.getLimit(), sbpo.getOrder_str());

                if (dt.getTotalRecords() <= 0)
                {
                    DataTable dt_1 = indexer.search(word, 0, 1, null);
                    dt.setTotalRecords(dt_1.getTotalRecords());
                }
                if (dt.getRows().size() > 0)
                {
                    sbpo.setOffset(0);
                    for (int i = 0; sbpo.getLimit() - ids.size() > 0 && i < dt.getRows().size(); i++)
                    {
                        String strID = dt.getRow(i).getString("ID");
                        if (!ids.contains(strID))
                        {// 判断重复
                            dtData.addCopyRow(dt.getRow(i));
                            ids.add(strID);
                        }
                        else
                        {
                            lngRowCount--;
                        }
                    }
                }
                else
                {
                    sbpo.setOffset(sbpo.getOffset() - (int) dt.getTotalRecords());
                    if (sbpo.getOffset() < 0)
                        sbpo.setOffset(0);
                }
                // 总记录
                lngRowCount += dt.getTotalRecords();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        dtData.setTotalRecords(lngRowCount);
        return dtData;
    }

    public static void main(String[] args)
    {
        // getData("postName: 硬件工程师   OR  postName: 工程师  OR  postName:深圳  OR  postName:高级  OR corpName:华为");
        // getData("intendTime: [0 TO 21001231]");
        // String s = "minExperience : [ 09 TO 10 ]";
//         String s = " yearPay : 100000-500000 ";
        // "intDeployTime : [ 20100101 TO 20100531 ] AND (postName: 事业部总经理^5  or postName:事业部^1 or postName:总经理^1)";

        SearchBasePO sbpo = new SearchBasePO();
        sbpo.setOffset(0);
        sbpo.setLimit(10);
//         sbpo.setParamStr(s);
//        sbpo.setOrder_str("intDeployTime desc");

//        sbpo.setParam("key", "市场策划经理");
//        sbpo.setParam("type", "postName");
        sbpo.setParam("key", "师");
        sbpo.setParam("type", "postName");
//        sbpo.setParam("areaID", 4403);
        // sbpo.setParam("areaID", 11);

        // for (String word : PostKeyAnalysisSupport.analysePostSearchKey(sbpo))
        // {
        // LogManager.debug(word);
        // sbpo.setParamStr(word);

        DataTable dt = getDataKeyAnalysis(sbpo);
        // DataTable dt2 = AceStringUtils.str2DataTable(dt.toXML(),
        // "DataTable");
        LogManager.debug("总条数：" + dt.getTotalRecords());
        for (DataRow dr : dt.getRows())
        {
//            LogManager.debug(dr.getString("ID") + "==" + dr.getString("areaID") + "职位名称：：" + dr.getString("postName")
//                    + "::" + dr.getString("corpName") + "::" + dr.getString("endTime") + "   :::   "
//                    + dr.getString("experience") + "   :::   " + dr.getString("studyLevel") + "   :::   "
//                    + dr.getString("strDeployTime") + "   :::   " + dr.getString("yearPay") + " ::: " + dr.getString("post_alias"));
        	LogManager.debug(dr.getString("postName"));
        }
        // }

        // LogManager.debug(dt2.toXML());
    }
}
